﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using BLL;
using DAL;
using Models;
using System.Data;
using System.Text;
using System.Xml;
using Gift.WebApp;

public partial class backstage_custom_navigation : System.Web.UI.Page
{
    // Fields

    protected CustomNavigation cn = new CustomNavigation();
    protected CustomNavigationManager cnm = new CustomNavigationManager();

    // Methods
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        string pathAndQuery = HttpContext.Current.Request.Url.PathAndQuery;
        try
        {
            this.cn.Title = this.txtTitle.Value;
            this.cn.Url = this.txtUrl.Value;
            this.cn.SortOrder = Convert.ToInt32(this.txtSortOrder.Value);
            this.cn.LanguageVersion = this.Session["language_version"].ToString();
            if ((base.Request.QueryString["ot"] == "ed") && (base.Request.QueryString["id"] != null))
            {
                this.cn.Id = Convert.ToInt32(base.Request.QueryString["id"]);
                if (this.cnm.ModifyCustomNavigation(this.cn))
                {
                    base.ClientScript.RegisterStartupScript(base.GetType(), "message", "<script language=\"javascript\" defer>alert(\"更新成功！\");location.href=\"custom_navigation_list.aspx\";</script>");
                }
                else
                {
                    base.ClientScript.RegisterStartupScript(base.GetType(), "message", "<script language=\"javascript\" defer>alert(\"更新失败！\");</script>");
                }
            }
            else if (this.cnm.AddCustomNavigation(this.cn))
            {
                base.ClientScript.RegisterStartupScript(base.GetType(), "message", "<script language=\"javascript\" defer>alert(\"添加成功！\");location.href=\"custom_navigation_list.aspx\";</script>");
            }
            else
            {
                base.ClientScript.RegisterStartupScript(base.GetType(), "message", "<script language=\"javascript\" defer>alert(\"添加失败！\");</script>");
            }
        }
        catch
        {
            if ((base.Request.QueryString["ot"] == "ed") && (base.Request.QueryString["id"] != null))
            {
                base.ClientScript.RegisterStartupScript(base.GetType(), "message", "<script language=\"javascript\" defer>alert(\"更新失败！\");</script>");
            }
            else
            {
                base.ClientScript.RegisterStartupScript(base.GetType(), "message", "<script language=\"javascript\" defer>alert(\"添加失败！\");</script>");
            }
        }
    }

    private void CustomNavigationBind()
    {
        try
        {
            DataTable customNavigationById = this.cnm.GetCustomNavigationById(Convert.ToInt32(base.Request.QueryString["id"]));
            if (customNavigationById.Rows.Count > 0)
            {
                DataRow row = customNavigationById.Rows[0];
                this.txtTitle.Value = row["title"].ToString();
                this.txtUrl.Value = row["url"].ToString();
                this.txtSortOrder.Value = row["sortOrder"].ToString();
            }
        }
        catch
        {
        }
    }

    public StringBuilder GetTitle()
    {
        StringBuilder builder = new StringBuilder();
        try
        {
            if (base.Request.QueryString["ot"] == null)
            {
                return builder;
            }
            if ((base.Request.QueryString["ot"] == "ed") && (base.Request.QueryString["id"] != null))
            {
                builder.Append("编辑导航");
                return builder;
            }
            builder.Append("添加导航");
        }
        catch
        {
        }
        return builder;
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if ((HttpContext.Current.Session["admin_user"] == null) && (HttpContext.Current.Session["language_version"] == null))
        {
            base.ClientScript.RegisterStartupScript(base.GetType(), "sJs", "top.location=\"login.html\";", true);
        }
        if ((!base.IsPostBack && (base.Request.QueryString["ot"] != null)) && ((base.Request.QueryString["ot"] == "ed") && (base.Request.QueryString["id"] != null)))
        {
            this.CustomNavigationBind();
        }
    }

}